package com.david.mydemo.ctl_commons;


import com.aliyun.oss.OSS;
import com.aliyuncs.IAcsClient;
import com.david.mydemo.framework.annotation.MySysLog;
import com.david.mydemo.util.MyConstants;
import com.david.mydemo.util.MyResponse;
import com.david.mydemo.util.OssFileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@RequestMapping(value = "/ossFiles")
@Api(tags = "OSS附件Controller")
public class OssFileController {

    @Autowired
    @Qualifier("ossFileClient")
    private OSS ossFileClient;

    @Autowired
    @Qualifier("ossAcsClient")
    private IAcsClient ossAcsClient;

    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;

    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @PostMapping("/ossUploadFile")
    @ApiOperation(value = "OSS上传附件")
    @MySysLog(operate = MyConstants.OPERATE_UPLOAD_FILE, content = "OSS上传附件", module = MyConstants.MODULE_COMMON)
    public MyResponse ossUploadFile(@RequestParam("file") MultipartFile file) {
        String fileName = OssFileUtil.uploadFile(file, endpoint, bucketName, ossFileClient);
        return new MyResponse().success(fileName);
    }

    @PostMapping("/ossDeleteFile")
    @ApiOperation(value = "OSS删除附件")
    @MySysLog(operate = MyConstants.OPERATE_DELETE_FILE, content = "OSS删除附件", module = MyConstants.MODULE_COMMON)
    public MyResponse ossDeleteFile(@RequestParam("fileName") String fileName) {
        OssFileUtil.deleteFile(fileName, bucketName, ossFileClient);
        return new MyResponse().success();
    }

}
